package weaver.interfaces.jjy.duss.util;

import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.JCO;
import com.weaver.general.BaseBean;

import java.util.Properties;


public class SAPConnPool {
    private final static String POOL_NAME = "Pool";
    private static String sapclient = "";
    private static String userid = "";
    private static String password = "";
    private static String hostname = "";
    private static String systemnumber = "";
    private static String Language = "";
    private static String group = "";
    private static String mshost = "";
    private static String r3name = "";
    private static int maxconn = 150;
    private JCO.Pool pool = null;

    private void init() {
        BaseBean baseBean = new BaseBean();
        try {
			/*sapclient = baseBean.getPropValue("SAPPool", "sapclient"); //要登录的客户端
			userid = baseBean.getPropValue("SAPPool", "userid"); //用户名
			password = baseBean.getPropValue("SAPPool", "password");  //登录口令
			hostname = baseBean.getPropValue("SAPPool", "hostname"); //系统的IP地址
			systemnumber = baseBean.getPropValue("SAPPool", "systemnumber");//系统编号
			Language = baseBean.getPropValue("SAPPool", "Language");  //系统语言
			mshost = baseBean.getPropValue("SAPPool", "mshost");  //消息服务器
			r3name = baseBean.getPropValue("SAPPool", "r3name");  //集群系统标识
			group = baseBean.getPropValue("SAPPool", "group");  //SAP集群组
			pool = JCO.getClientPoolManager().getPool(POOL_NAME);*/

           /*
            sapclient = "800";
            userid = "HR_CON_ADM";
            password = "SAP@s4dev";
            hostname = "192.168.200.11";
            systemnumber = "00";
            Language = "ZH";
            maxconn = 150;
            mshost = "192.168.200.11";
            r3name = "S4P";
            group = "S4P";*/

                    /*sapclient="100";
                    userid="RFC_SYS";
                    password="init123";
                    hostname="172.27.3.84";
                    systemnumber="10";
                    Language="ZH";
                    maxconn=150;*/
            sapclient="800";
            userid="RFC_SYS";
            password="init123";
            hostname="172.27.9.188";
            systemnumber="20";
            Language="ZH";
            maxconn=150;


                  /*sapclient="300";
                    userid="HR_CON_ADM";
                    password="123456";
                    hostname="192.168.200.18";
                    systemnumber="00";
                    Language="ZH";
                    maxconn=150;*/

            pool = JCO.getClientPoolManager().getPool(POOL_NAME);
            if (pool == null) {
                Properties logonProperties = new Properties();
                logonProperties.put("jco.client.ashost", hostname);
                logonProperties.put("jco.client.client", sapclient);
                logonProperties.put("jco.client.sysnr", systemnumber);
                logonProperties.put("jco.client.user", userid);
                logonProperties.put("jco.client.passwd", password);
                logonProperties.put("jco.client.lang", Language);
                logonProperties.put("jco.client.mshost", mshost);
                logonProperties.put("jco.client.r3name", r3name);
                logonProperties.put("jco.client.group", group);
                //JCO.addClientPool(POOL_NAME, maxconn, sapclient, userid, password, Language, hostname, systemnumber,group,post);
                JCO.addClientPool(POOL_NAME, maxconn, logonProperties);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void releaseC(JCO.Client Client) {
        if (Client != null) {
            JCO.releaseClient(Client);
        }
    }

    public JCO.Client getConnection() {
        //判断连接池是否为NULL
        if (pool == null) {
            init();
        }
        JCO.Client Client = null;
        try {
            Client = JCO.getClient(POOL_NAME);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Client;
    }

    public JCO.Function excuteBapi(String s) {
        SAPConnPool SAPConn = new SAPConnPool();
        JCO.Client sapconnection = SAPConn.getConnection();
        JCO.Repository mRepository;
        JCO.Function jcoFunction = null;
        if (sapconnection == null) {
            return jcoFunction;
        }
        try {
            mRepository = new JCO.Repository("Repository", sapconnection);
            IFunctionTemplate ft = mRepository.getFunctionTemplate(s);
            jcoFunction = new JCO.Function(ft);
            SAPConn.releaseC(sapconnection);
            return jcoFunction;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            SAPConn.releaseC(sapconnection);
        }
    }
}
